package com.yq.ylmall.product.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yq.ylmall.dto.product.ProductDecreaseDto;
import com.yq.ylmall.model.product.Banner;
import com.yq.ylmall.product.service.IBannerService;
import com.yq.ylmall.product.service.ICategoryService;
import com.yq.ylmall.product.service.IProductService;
import com.yq.ylmall.vo.product.ProductInfoVo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/product")
public class ProductApiController {

    private final IProductService productService;
    private final ICategoryService categoryService;
    private final IBannerService bannerService;

    //根据商品Id获取商品信息
    @GetMapping("/get/{id}")
    public ProductInfoVo getById(@PathVariable("id") Long id) {
        return productService.getProductInfoById(id);
    }


    @PostMapping("/decrease")
    public void decreaseStock(@RequestBody ProductDecreaseDto productDecrease) {
        productService.decreaseStock(productDecrease);
    }

    @ApiOperation(value = "首页分类数据")
    @GetMapping("/category/index")
    public String indexCategory() {
        return categoryService.findAll();
    }

    @ApiOperation(value = "获取启用的Banner列表")
    @GetMapping("/banner")
    public List<Banner> indexBanner() {
        return bannerService.list(Wrappers.<Banner>lambdaQuery().eq(Banner::getStatus, 1).orderByDesc(Banner::getSort));
    }

}
